<?php

namespace app\order\logic;
use think\facade\Db;
class OrderListLogic
{
    /**
     * @desc    订单数据组装
     * @date    2024-04-10
     * @author  luckyXiong
     */
    public function get_arr_info($info){

        $order_arr  = [
            'order_id'                       => $info['id']??'',
            'shop_id'                        => $info['shop_id']??'',
            'createtime'                     => $info['createtime'],
            'updatetime'                     => $info['update_time']??'',
            'paid_time'                      => $info['paid_time']??'',
            'delivery_time'                  => $info['delivery_time']??'',
            'fulfillment_type'               => $info['fulfillment_type']??'',
            'status'                         => $info['status']??'',
            'shipping_type'                  => $info['shipping_type']??'',
            'tracking_number'                => $info['tracking_number']??'',
            'currency'                       => $info['payment']['currency']??'',
            'sub_total'                      => $info['payment']['sub_total']??'',
            'tax'                            => $info['payment']['tax']??'',
            'total_amount'                   => $info['payment']['total_amount']??'',
            'shipping_fee_platform_discount' => $info['payment']['shipping_fee_platform_discount']??'',
            'original_total_product_price'   => $info['payment']['original_total_product_price']??'',
            'platform_discount'              => $info['payment']['platform_discount']??'',
            'shipping_fee'                   => $info['payment']['shipping_fee']??'',
        ];

        //订单商品信息;
        foreach($info['line_items'] as $line_item){
            $spu = $line_item['seller_sku'] ?  get_spu_info($line_item['seller_sku']):'';
            $order_info_arr[] = [
                'order_id'        => $info['id']??'',
                'shop_id'         => $info['shop_id']??'',
                'info_id'         => $line_item['id']??'',
                'product_id'      => $line_item['product_id']??'',
                'product_name'    => $line_item['product_name']??'',
                'sku_id'          => $line_item['sku_id']??'',
                'seller_sku'      => $line_item['seller_sku']??'',
                'spu'             => $spu,
                'createtime'      => $info['createtime'],
                'display_status'  => $line_item['display_status']??'',
                'cancel_user'     => $line_item['cancel_user']??'',
                'cancel_reason'   => $line_item['cancel_reason']??'',
                'num'             => get_colours_num($line_item['seller_sku']),
                'sale_price'      => $line_item['sale_price']??'',
                'package_id'      => $line_item['package_id']??'',
                'package_status'  => $line_item['package_status']??'',
            ];
        }

        //附加订单信息;
        if (!empty($info['recipient_address']['district_info'])) {
            $district_info = $info['recipient_address']['district_info'];
            foreach ($district_info as $kd => $vd) {
                switch ($vd['address_level_name']) {
                    case 'Country':
                        $append_arr['country'] = $vd['address_name'];
                        break;
                    case 'Federal District':
                        $append_arr['state'] = $vd['address_name'];
                        break;
                    case 'State':
                        $append_arr['state'] = $vd['address_name'];
                        break;
                    case 'County':
                        $append_arr['county'] = $vd['address_name'];
                        break;
                    case 'City':
                        $append_arr['city'] = $vd['address_name'];
                        break;
                    default:
                }
            }
        }


        $order_append_arr = [
            'order_id'                       => $info['id']??'',
            'shop_id'                        => $info['shop_id']??'',
            'shipping_provider'              => $info['shipping_provider']??'',
            'shipping_provider_id'           => $info['shipping_provider_id']??'',
            'buyer_email'                    => $info['buyer_email']??'',
            'name'                           => $info['recipient_address']['name']??'',
            'phone_number'                   => $info['recipient_address']['phone_number']??'',
            'postal_code'                    => $info['recipient_address']['postal_code']??'',
            'region_code'                    => $info['recipient_address']['region_code']??'',
            'delivery_option_name'           => $info['delivery_option_name']??'',
            'payment_method_name'            => $info['payment_method_name']??'',
            'country'                        =>$append_arr['country']??'',
            'state'                          =>$append_arr['state']??'',
            'county'                         =>$append_arr['county']??'',
            'city'                           =>$append_arr['city']??'',
            'address_detail'                 =>$info['recipient_address']['address_detail']??'',
            'delivery_option_id'             =>$info['delivery_option_id']??'',
            'user_id'                        =>$info['user_id']??'',
            'warehouse_id'                   =>$info['warehouse_id']??'',
            'json'                           =>json_encode($info,JSON_UNESCAPED_UNICODE),
        ];


        return [
            'order_arr'       => $order_arr,
            'order_info_arr'  => $order_info_arr,
            'order_append_arr'=> $order_append_arr,
        ];
    }


    /**
     * @desc  获取待更新的订单
     * @date  2024-4-12
     */
    public function get_update_info($info){
        $order_arr  = $order_info_arr = $order_append_arr = $order_refund = $order_cancel = $append_arr = [];
        $order_arr  = [
            'createtime'                     => $info['createtime']??'',
            'paid_time'                      => $info['paid_time']??'',
            'delivery_time'                  => $info['delivery_time']??'',
            'fulfillment_type'               => $info['fulfillment_type']??'',
            'status'                         => $info['status']??'',
            'shipping_type'                  => $info['shipping_type']??'',
            'tracking_number'                => $info['tracking_number']??'',
            'currency'                       => $info['payment']['currency']??'',
            'sub_total'                      => $info['payment']['sub_total']??'',
            'tax'                            => $info['payment']['tax']??'',
            'total_amount'                   => $info['payment']['total_amount']??'',
            'shipping_fee_platform_discount' => $info['payment']['shipping_fee_platform_discount']??'',
            'original_total_product_price'   => $info['payment']['original_total_product_price']??'',
            'platform_discount'              => $info['payment']['platform_discount']??'',
            'shipping_fee'                   => $info['payment']['shipping_fee']??'',
            'updatetime_order'               => time(),
        ];

        //订单商品信息;
        foreach($info['line_items'] as $line_item){
            Db::name('order_info')->where('info_id',$line_item['id'])->update(
                [
                    'order_id'        => $info['id']??'',
                    'shop_id'         => $info['shop_id']??'',
                    'info_id'         => $line_item['id']??'',
                    'product_id'      => $line_item['product_id']??'',
                    'product_name'    => $line_item['product_name']??'',
                    'sku_id'          => $line_item['sku_id']??'',
                    'seller_sku'      => $line_item['seller_sku']??'',
                    'display_status'  => $line_item['display_status']??'',
                    'cancel_user'     => $line_item['cancel_user']??'',
                    'cancel_reason'   => $line_item['cancel_reason']??'',
                    'sale_price'      => $line_item['sale_price']??'',
                    'package_id'      => $line_item['package_id']??'',
                    'package_status'  => $line_item['package_status']??'',
                ]
            );
        }

        //附加订单信息;
        if (!empty($info['recipient_address']['district_info'])) {
            $district_info = $info['recipient_address']['district_info'];
            foreach ($district_info as $vd) {
                switch ($vd['address_level_name']) {
                    case 'Country':
                        $append_arr['country'] = $vd['address_name'];
                        break;
                    case 'Federal District':
                        $append_arr['state'] = $vd['address_name'];
                        break;
                    case 'State':
                        $append_arr['state'] = $vd['address_name'];
                        break;
                    case 'County':
                        $append_arr['county'] = $vd['address_name'];
                        break;
                    case 'City':
                        $append_arr['city'] = $vd['address_name'];
                        break;
                    default:
                }
            }
        }


        $order_append_arr = [
            'shipping_provider'              => $info['shipping_provider']??'',
            'shipping_provider_id'           => $info['shipping_provider_id']??'',
            'buyer_email'                    => $info['buyer_email']??'',
            'name'                           => $info['recipient_address']['name']??'',
            'phone_number'                   => $info['recipient_address']['phone_number']??'',
            'postal_code'                    => $info['recipient_address']['postal_code']??'',
            'region_code'                    => $info['recipient_address']['region_code']??'',
            'delivery_option_name'           => $info['delivery_option_name']??'',
            'payment_method_name'            => $info['payment_method_name']??'',
            'country'                        =>$append_arr['country']??'',
            'state'                          =>$append_arr['state']??'',
            'county'                         =>$append_arr['county']??'',
            'city'                           =>$append_arr['city']??'',
            'address_detail'                 =>$info['recipient_address']['address_detail']??'',
            'delivery_option_id'             =>$info['delivery_option_id']??'',
            'user_id'                        =>$info['user_id']??'',
            'warehouse_id'                   =>$info['warehouse_id']??'',
            'json'                           =>json_encode($info,JSON_UNESCAPED_UNICODE),
        ];


        return [
            'order_arr'       => $order_arr,
            'order_append_arr'=> $order_append_arr,
        ];
    }




}